package view.component.monster.large_monster
{
	import embeds.EmbedRes;
	
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.Screen;
	import feathers.controls.TextInput;
	
	import model.vo.monster.SpVo;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;
	
	import util.FG2FUGenerator;
	
	import view.event.monster.LMVideoEditViewEvent;
	
	
	/**
	 * 大型怪物详细-视频收藏-编辑界面
	 * @author £iang
	 * 
	 */	
	public class LMVideoEditView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 名称标签 */
		public var nameLabel:Label;
		/** 地址标签 */
		public var urlLabel:Label;
		/** 评分标签 */
		public var starLabel:Label;
		/** 名称输入 */
		public var nameInput:TextInput;
		/** 地址输入 */
		public var urlInput:TextInput;
		/** 主组 */
		public var mainGroup:Sprite;
		/** 星组 */
		public var starGroup:Sprite;
		/** 无星图片组 */
		public var noStarVec:Vector.<Image>;
		/** 星星图片组 */
		public var starVec:Vector.<Image>;
		/** 确定按钮 */
		public var ensureBtn:Button;
		/** 取消按钮 */
		public var cancelBtn:Button;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 当前星数 */
		private var _starNum:int;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			mainGroup = new Sprite();
			this.addChild( mainGroup );
			
			nameLabel = new Label();
			nameLabel.text = "名称："
			mainGroup.addChild( nameLabel );
			
			nameInput = FG2FUGenerator.generateRoundRectTextInput(
				250, 50, this.dpiScale, 12 
			);
			mainGroup.addChild( nameInput );
			
			urlLabel = new Label();
			urlLabel.text = "网址：";
			mainGroup.addChild( urlLabel );
			
			urlInput = FG2FUGenerator.generateRoundRectTextInput(
				250, 50, this.dpiScale, 12 
			);
			mainGroup.addChild( urlInput );
			
			starLabel = new Label();
			starLabel.text = "评分：";
			mainGroup.addChild( starLabel );
			
			starGroup = new Sprite();
			mainGroup.addChild( starGroup );
			
			var i:int;
			var tmpImage:Image;
			var tmpTexture:Texture
			
			noStarVec = new Vector.<Image>;
			starVec = new Vector.<Image>;
			
			for ( i = 0; i < 5; i ++ )
			{
				tmpTexture = Texture.fromBitmapData( new EmbedRes.NOSTAR_ICON().bitmapData );
				tmpImage = new Image( tmpTexture );
				tmpImage.x = i * 46 * this.dpiScale;
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				noStarVec.push( tmpImage );
				starGroup.addChild( tmpImage );
				tmpTexture = Texture.fromBitmapData( new EmbedRes.STAR_ICON().bitmapData );
				tmpImage = new Image( tmpTexture );
				tmpImage.x = i * 46 * this.dpiScale;
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				starVec.push( tmpImage );
				starGroup.addChild( tmpImage );
			}
			
			setStar( 0 );
			
			ensureBtn = FG2FUGenerator.generateRoundRectButton(
				90, 50, this.dpiScale, 12, "small"
			);
			ensureBtn.label = "确定";
			mainGroup.addChild( ensureBtn );
			
			cancelBtn = FG2FUGenerator.generateRoundRectButton(
				90, 50, this.dpiScale, 12, "small"
			);
			cancelBtn.label = "取消";
			mainGroup.addChild( cancelBtn );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			nameInput.x = 110 * this.dpiScale;
			urlLabel.y = 70 * this.dpiScale;
			urlInput.x = 110 * this.dpiScale;
			urlInput.y = 70 * this.dpiScale;
			starLabel.y = 140 * this.dpiScale;
			starGroup.x = 110 * this.dpiScale;
			starGroup.y = 140 * this.dpiScale;
			ensureBtn.y = 250 * this.dpiScale;
			ensureBtn.x = 75 * this.dpiScale;
			cancelBtn.y = 250 * this.dpiScale;
			cancelBtn.x = 185 * this.dpiScale;
			mainGroup.y = 20 * this.dpiScale;
			mainGroup.x = ( UISize.MAIN_W - 350 * this.dpiScale ) / 2;
		}
		
		/**
		 * 清除视频数据 
		 * 
		 */		
		public function clearInfo():void
		{
			nameInput.text = "";
			urlInput.text = "";
			setStar( 0 );
		}
		
		/**
		 * 设置视频数据 
		 * @param data 数据
		 * 
		 */		
		public function setInfo( data:SpVo ):void
		{
			nameInput.text = data.name;
			urlInput.text = data.url;
			setStar( data.star );
		}
		
		/**
		 * 获取编辑或添加后的视频数据 
		 * @return 视频数据
		 * 
		 */		
		public function getEditInfo():SpVo
		{
			var spVo:SpVo = new SpVo();
			
			spVo.name = nameInput.text;
			spVo.url = urlInput.text;
			spVo.star = _starNum;
			
			return spVo;
		}
		
		/**
		 * 设置星星 
		 * @param num 星数
		 * 
		 */		
		public function setStar( num:int ):void
		{
			_starNum = num;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				starVec[ i ].visible = ( i < num );
			}
		}
		
		public function addListener():void
		{
			starGroup.addEventListener( TouchEvent.TOUCH, starTouchHandler );
			ensureBtn.addEventListener( Event.TRIGGERED, ensureTriggeredHandler );
			cancelBtn.addEventListener( Event.TRIGGERED, cancelTriggeredHandler );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理星组被触摸时派发的事件 
		 * @param event 星组被触摸时派发的事件 
		 * 
		 */		
		protected function starTouchHandler( event:TouchEvent ):void
		{
			var touch:Touch = event.getTouch( this );
			
			if ( touch && touch.phase != TouchPhase.HOVER )
			{
				var offsetX:Number = touch.getPreviousLocation( starGroup ).x;
				var starNum:int = int( Math.ceil( offsetX / ( 46.5 * this.dpiScale ) ) );
				
				if ( starNum != _starNum )
				{
					setStar( starNum );
				}
			}
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */		
		public function playHideAnimation():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0);
			Starling.juggler.add( tween );
		}
		
		/**
		 * 处理点击确认按钮后派发的事件 
		 * @param event 点击确认按钮后派发的事件 
		 * 
		 */		
		protected function ensureTriggeredHandler( event:Event ):void
		{
			dispatchCommonEvent(
				new LMVideoEditViewEvent( LMVideoEditViewEvent.ENSURE )
			);
		}
		
		/**
		 * 处理点击取消按钮后派发的事件 
		 * @param event 点击取消按钮后派发的事件 
		 * 
		 */		
		protected function cancelTriggeredHandler( event:Event ):void
		{
			dispatchCommonEvent(
				new LMVideoEditViewEvent( LMVideoEditViewEvent.CANCEL )
			);
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( this );
			
			nameLabel.dispose();
			nameLabel = null;
			urlLabel.dispose();
			urlLabel = null;
			starLabel.dispose();
			starLabel = null;
			
			nameInput.dispose();
			FG2FUGenerator.deepDisposeTextInputComp( nameInput );
			nameInput = null;
			urlInput.dispose();
			FG2FUGenerator.deepDisposeTextInputComp( urlInput );
			urlInput = null;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				( noStarVec[ i ] as Image ).dispose();
				( noStarVec[ i ] as Image ).texture.dispose();
				noStarVec[ i ] = null;
				( starVec[ i ] as Image ).dispose();
				( starVec[ i ] as Image ).texture.dispose();
				starVec[ i ] = null;
			}
			
			noStarVec.length = 0;
			noStarVec = null;
			
			starVec.length = 0;
			starVec = null;
			
			starGroup.dispose();
			starGroup = null;
			
			ensureBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( ensureBtn );
			ensureBtn = null;
			
			cancelBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( cancelBtn );
			cancelBtn = null;
			
			mainGroup.dispose();
			mainGroup = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LMVideoEditView()
		{
			super();
		}
	}
}